<?php
/**
 *
 * 版权所有：顺势网站管理系统<www.51shunshi.com>

 * 日    期：2016-01-21
 * 版    本：1.0.0
 * 功能说明：前台控制器演示。
 *
 **/
namespace Home\Controller;

use Vendor\Page;
use EasyWeChat\Foundation\Application;
class IndexController extends ComController
{
    public function index()
    {
        $banner_db = M('photos');
        $category_db = M('category');
        $article_db = M('article as a');
        $links_db = M('links');
        $prefix = C('DB_PREFIX');
        //meta关键词&描述
        $metalist = M("setting")
            ->field("v")
            ->where(array('k'=>array('in', 'keywords,description')))
            ->select();
        $this->assign("metaDes",$metalist['0']['v']);
        $this->assign("metaKeywords",$metalist['1']['v']);

        //轮播图
        $banner = $banner_db
                ->field("title,link,desc,pic_url")
                ->where("category=1")
                ->select();
        $this->assign("banner",$banner);
        $this->assign("bannerphon",$banner);
        $this->assign("bannernav",$banner);
        //轮播图右1T
        $bannerR1T = $banner_db
            ->field("title,link,desc,pic_url")
            ->where("category=2")
            ->order('id desc')
            ->limit(1)
            ->select();
        $this->assign("bannerR1T",$bannerR1T['0']);
        //轮播图右1B
        $bannerR1B = $banner_db
            ->field("title,link,desc,pic_url")
            ->order('id desc')
            ->limit(1)
            ->where("category=3")
            ->select();
        $this->assign("bannerR1B",$bannerR1B['0']);
        //轮播图右2
        $bannerR2 = $banner_db
            ->field("title,link,desc,pic_url,time")
            ->where("category=4")
            ->order('id desc')
            ->limit(1)
            ->select();
        $this->assign("bannerR2",$bannerR2['0']);



        //右侧广告图
        $rightAdvPic = $banner_db
            ->where(array("category"=>9))
            ->field("pic_url,link")
            ->order("id DESC")
            ->select();
        $this->assign("rightAdvPic",$rightAdvPic['0']);
        //文章

        $field = "a.aid,a.title,a.description,a.thumbnail,a.t,a.point,a.browse,{$prefix}category.name,{$prefix}category.dir";
        $where = "a.sid != 43";
        $articleicfo = $article_db
            ->field($field)
            ->order("aid desc")
            ->join("{$prefix}category ON {$prefix}category.id = a.sid")
            ->where($where)
            ->limit(20)
            ->select();

        for ($i= 0;$i<count($articleicfo);$i++){
            foreach ($articleicfo[$i] as $k=>$v){
                if($k == t){
                    $time = mdate(strtotime($v),now);
                    $articleicfo[$i]["time"] = $time;
                }
                if($k == description ){
                    if(strlen($v)>66){
                        $articleicfo[$i]["description"] = mb_substr($v,0,66)."...";
                    }
                }
            }
        }
        $this->assign("articleicfo",$articleicfo);

        // 最近七天点击最多的栏目文章列表
        $hotListLog = $this->hotList();
        if(!empty($hotListLog)){
            foreach ($hotListLog as $key => $val){
                $hotList[$key]['title'] = $article_db->where("aid=".$val['aid'])->getField("title");
                $hotList[$key]['aid'] = $val['aid'];
                $hotList[$key]['dir'] =  $val['dir'];
            }
            $this->assign("hotList",$hotList);
        }

        //友情链接
        $linksList = $links_db
            ->field("title,url")
            ->select();
        $this->assign("friendLink",$linksList);
        // 微信浏览器
        if(strstr($_SERVER['HTTP_USER_AGENT'],'MicroMessenger')){
            $config = [
                'app_id' => C("AppId"),
                'secret' => C("AppSecret"),
            ];
            $app = new Application($config);
            $js = $app->js;
            $this->assign('iswechat', 1);
            $this->assign('js', $js);
        }
        if(isMobile()){
            $this->display("Mobile/index");
        }else{
          $this->display();  
        } 
    }

    public function circleCode(){
        $id = $_POST['id'];

        $photos_db = M("photos");

        $photoscon = $photos_db
            ->where(array("category"=>$id))
            ->field("title,desc,pic_url")
            ->order("id DESC")
            ->select();
       $this->ajaxReturn($photoscon['0'],"JSON");

    }

    public function loadmore(){
        $num = $_POST['num'];
        $article_DB = M("article as a");
        $prefix = C('DB_PREFIX');
        $field = "a.aid,a.title,a.description,a.thumbnail,a.t,a.point,a.browse,{$prefix}category.name,{$prefix}category.dir";

        $articleicfo = $article_DB
            ->field($field)
            ->order("aid desc")
            ->join("{$prefix}category ON {$prefix}category.id = a.sid")
            ->limit($num,10)
            ->select();
        for ($i= 0;$i<count($articleicfo);$i++){
            foreach ($articleicfo[$i] as $k=>$v){
                if($k == t){
                    $time = mdate(strtotime($v),now);
                    $articleicfo[$i]["time"] = $time;
                }
                if($k == description ){
                    if(strlen($v)>66){
                        $articleicfo[$i]["description"] = mb_substr($v,0,66)."...";
                    }
                }
            }
        }
            $this->ajaxReturn($articleicfo,"JSON");


        //dump($articleicfo);
    }
}